﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using System.Net;
using System.Text;

namespace dotnetsys
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                Response.Write(Request.QueryString["id"]);
            }
            /*
            string pwd = "asdf234";
            if (!Regex.IsMatch(pwd, "^[a-zA-Z0-9]{6,20}$",RegexOptions.None))
            {
                Response.Write("<script language='JavaScript'>alert(' 长度6,20个字符，只能为英语字、数字')</script>");
                return;
            }
            else
            {
                Response.Write("<script language='JavaScript'>alert('ok')</script>");
                Response.End();// return;
            }

            */
            /*
            string urls = "", titles = "", yourStr = @"<a href=""html/1.html"">第一个页面</a><a href=""html/2.html"">第2个页面</a><a href=""html/3.html"">第3个页面</a>";
            Regex reg = new Regex(@"(?is)<a[^>]*?href=(['""]?)(?<url>[^'""\s>]+)\1[^>]*>(?<text>(?:(?!</?a\b).)*)</a>");
            MatchCollection mc = reg.Matches(yourStr);
            foreach (Match m in mc)
            {
                urls += m.Groups["url"].Value + "=";
                titles += m.Groups["text"].Value + "=";
            }

            Response.Write(urls + "," + titles);

            */
        }


        //// <summary>
        /// 获取网页源代码
        /// </summary>
        /// <param name="url">URL路径</param>
        /// <returns></returns>
        public string GetHTML(string url, string encoding)
        {
            WebClient web = new WebClient();
            web.Encoding = Encoding.GetEncoding(encoding) ;
            byte[] buffer = web.DownloadData(url);           
            //return Encoding.Default.GetString(buffer);
            return System.Text.Encoding.UTF8.GetString(buffer); 
        }

        //

        private void caiji()
        {
            //网页操作对象,我用来获取网页源码

            //HTML html = new HTML();
            //对博客园每日排行数据进行采集
            string htmlcode = GetHTML("http://www.cnblogs.com/TopPosts.aspx", "utf-8");
            //提取博客园排行文章信息的正则表达式
            Regex regexarticles = new Regex(".+· <a\\s+id=\".+\" href=\"(?<url>.+)\"\\s+target=\"_blank\">(?<title>.+)</a> <span\\s+class=\".+\">\\(阅读:(?<views>\\d+)\\).*\\(评论:(?<reply>\\d+)\\).*\\((?<time>.+)\\)</span>\\s*</td>\\s*<td\\s+height=\"\\d+\">\\s+<a\\s+id=\".+\" href=\"(?<blog>.+)\">(?<author>.+)</a>");

            /*
            //所有匹配表达式的内容
            MatchCollection marticles = regexarticles.Matches(htmlcode);                        
            ///遍历匹配内容
            foreach (Match m in marticles)
            {

                Entity.Article test = new Entity.Article();

                test.Category = "博客园热门文章";          //设置分类

                test.Title = m.Groups["title"].Value;      //设置标题

                test.Url = m.Groups["url"].Value;          //设置连接

                test.Views = int.Parse(m.Groups["views"].Value);    //设置浏览次数

                test.Replys = int.Parse(m.Groups["reply"].Value);  //设置评论次数

                test.Datatime = m.Groups["time"].Value;            //设置发布时间

                test.Author = m.Groups["author"].Value;            //设置作者

                test.Site = m.Groups["blog"].Value;                //设置文章出处

                list.Add(test);

            }
            */
            MatchCollection marticles = regexarticles.Matches(htmlcode);
            foreach (Match m in marticles)
            {
                Literal1.Text = m.Groups["title"].Value;
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            caiji();
        }

    }
}
